//
//  LRXSelectTimeInputView.m
//  路人侠
//
//  Created by zhangxulong on 4/6/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXSelectTimeInputView.h"
@interface LRXSelectTimeInputView()<UIPickerViewDelegate,UIPickerViewDataSource>


@end
@implementation LRXSelectTimeInputView

- (void)selectTimeInputView:(NSArray *)selectTime
{
    self.backgroundColor = [UIColor whiteColor];
    
    
    UIPickerView * stHoutView = [[UIPickerView alloc]init];
    UIPickerView * stMinuView = [[UIPickerView alloc]init];
    UIPickerView * endHoutView = [[UIPickerView alloc]init];
    UIPickerView * endMinuView = [[UIPickerView alloc]init];
    UILabel * startTime = [[UILabel alloc]init];
    UILabel * endTime = [[UILabel alloc]init];
    
    startTime.textColor = orangeColor;
    endTime.textColor = orangeColor;
    endTime.text = @"最晚送达时间";
    startTime.text = @"最早送达时间";
    startTime.font = [UIFont systemFontOfSize:20 * hScale];
    endTime.font = startTime.font;
    
    [startTime sizeToFit];
    [endTime sizeToFit];
    
    
    stHoutView.delegate = self;
    stHoutView.dataSource = self;
    stMinuView.delegate = self;
    stMinuView.dataSource = self;
    endMinuView.delegate = self;
    endMinuView.dataSource = self;
    endHoutView.delegate = self;
    endHoutView.dataSource = self;
    
    stHoutView.tag = 0;
    stMinuView.tag = 1;
    endHoutView.tag = 2;
    endMinuView.tag = 3;
    
    
    CGFloat stHoutViewX = 90 * wScale;
   
    CGFloat pickViewY = 50 * hScale;
    CGFloat pickViewW = 90 * wScale;
    CGFloat pickViewH = self.frame.size.height - 80 * hScale;
     CGFloat interval = (MainScreenW - stHoutViewX * 2 - 4 * pickViewW) / 4;
    
    stHoutView.frame = CGRectMake(stHoutViewX, pickViewY, pickViewW,pickViewH );
    
    
    UILabel * maohao1 = [[UILabel alloc]init];
    maohao1.text = @":";
    maohao1.font = [UIFont systemFontOfSize:28 * hScale];
    maohao1.textColor = orangeColor;
    maohao1.frame = CGRectMake(CGRectGetMaxX(stHoutView.frame), pickViewY, interval, pickViewH);
    maohao1.textAlignment = NSTextAlignmentCenter;
    
    CGFloat stMinuViewX = CGRectGetMaxX(stHoutView.frame) + interval;
    stMinuView.frame = CGRectMake(stMinuViewX, pickViewY, pickViewW, pickViewH);
    
    UILabel * fenhao = [[UILabel alloc]init];
    fenhao.text = @" - ";
    fenhao.font = [UIFont systemFontOfSize:28 * hScale];
    fenhao.textColor = orangeColor;
    fenhao.frame = CGRectMake(CGRectGetMaxX(stMinuView.frame), pickViewY, interval, pickViewH);
    fenhao.textAlignment = NSTextAlignmentCenter;
    
    
    CGFloat endHoutViewX = CGRectGetMaxX(stMinuView.frame) + interval;
    endHoutView.frame = CGRectMake(endHoutViewX, pickViewY, pickViewW, pickViewH);
    
    UILabel * maohao2 = [[UILabel alloc]init];
    maohao2.text = @":";
    maohao2.font = [UIFont systemFontOfSize:28 * hScale];
    maohao2.textColor = orangeColor;
    maohao2.textAlignment = NSTextAlignmentCenter;
    maohao2.frame = CGRectMake(CGRectGetMaxX(endHoutView.frame), pickViewY, interval, pickViewH);

    
    CGFloat endMinuViewX = CGRectGetMaxX(endHoutView.frame) + interval;
    endMinuView.frame = CGRectMake(endMinuViewX, pickViewY, pickViewW, pickViewH);
    
    CGFloat minY = 40 * hScale;
    
    [startTime sizeToFit];
    [endTime sizeToFit];
    
    startTime.center = CGPointMake(maohao1.center.x, minY);
    endTime.center = CGPointMake(maohao2.center.x, minY);
  
    
    
    [self addSubview:startTime];
    [self addSubview:endTime];
    [self addSubview:stMinuView];
    [self addSubview:maohao1];
    [self addSubview:stHoutView];
    [self addSubview:fenhao];
    [self addSubview:endMinuView];
    [self addSubview:maohao2];
    [self addSubview:endHoutView];
    
    
    NSInteger stHour = [selectTime[0] integerValue];
    NSInteger stMinu = [selectTime[1] integerValue];
    NSInteger endHour = [selectTime[2] integerValue];
    NSInteger endMinu = [selectTime[3] integerValue];
    
  
    
    [stHoutView selectRow:stHour - 1 inComponent:0 animated:NO];
    [stMinuView selectRow:stMinu - 1 inComponent:0 animated:NO];
    [endHoutView selectRow:endHour - 1 inComponent:0 animated:NO];
    [endMinuView selectRow:endMinu - 1 inComponent:0 animated:NO];
        

    
    
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == 0 || pickerView.tag == 2) {
        return 24;
    }
    else
    {
        return 60;
    }
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel * textLabel = [[UILabel alloc]init];
    textLabel.text = [NSString stringWithFormat:@"%02ld",row];
    textLabel.font = [UIFont systemFontOfSize:28 * hScale];
    textLabel.textColor = mainColor;
    textLabel.textAlignment = NSTextAlignmentCenter;
    return textLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
    UILabel * selectItem = (UILabel *)[pickerView viewForRow:row forComponent:component];
    selectItem.textColor = orangeColor;
    if (pickerView.tag == 0) {
        self.stHour = row;
    }
    if (pickerView.tag == 1) {
        self.stMinu = row;
    }
    if (pickerView.tag == 2) {
        self.endHour = row;
    }
    if (pickerView.tag == 3) {
        self.endMinu = row;
    }
    if([self.selectTimeDelegate respondsToSelector:@selector(selectTimeInputView:withInputViewTag:)])
    {
        [self.selectTimeDelegate selectTimeInputView:self withInputViewTag:pickerView.tag];
    }

}

@end
